package com.jy.adscm.dealer.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jy.adscm.dealer.bean.DealerAttachment;
import com.jy.adscm.dealer.bean.DealerMembers;
import com.jy.adscm.dealer.mapper.DealerAttachmentMapper;
import com.jy.adscm.dealer.service.DealerAttachmentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.dealer.service.DealerInfoService;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.utils.AdscmSystemUtils;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-01-16
 */
@Service
public class DealerAttachmentServiceImpl extends ServiceImpl<DealerAttachmentMapper, DealerAttachment> implements DealerAttachmentService {
    @Autowired
    DealerInfoService dealerInfoService;
    @Autowired
    DealerAttachmentMapper  dealerAttachmentMapper;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Transactional
    @Override
    public boolean insertDealerAttachment(HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        String dealerAttachmentstr = request.getParameter("dealerAttachment");
        if (dealerAttachmentstr==null||"".equals(dealerAttachmentstr)){
            throw  new AdscmSystemException("上传信息不全");
        }
        ObjectMapper mapper = new ObjectMapper();
        DealerAttachment dealerAttachment=new DealerAttachment();
        try {
            dealerAttachment   = mapper.readValue(dealerAttachmentstr,DealerAttachment.class);

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!AdscmBaseUtils.getInstance().isNotEmpty(dealerAttachment,null)){
            throw  new AdscmSystemException("上传信息不正确");
        }
        List<String> paths = null;
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());
        if(multipartResolver.isMultipart(request)){
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
            String path =    AdscmBaseUtils.getInstance().getUploadpath()+"image"+File.separator+ UUID.randomUUID().toString().replaceAll("-","") + File.separator;
            System.out.println(path);
            String[] split = path.split("\\\\");
            if (split[0].equals("file:")){
                StringBuffer  sb =new StringBuffer();
                for (int i = 1; i < split.length; i++) {
                    if (i==split.length){
                        sb.append(split[i]);
                    }else {
                        sb.append(split[i]+"\\");
                    }
                }
                path=sb.toString();
            }
            System.out.println(path);
            paths = AdscmSystemUtils.getInstance().uploadFileNoNewName(multiRequest,path);
        }
        if (paths!=null){
            String path = paths.get(0);
            dealerAttachment.setPath(path);
        }
        dealerAttachment.setCreateTime(new Date());
        dealerAttachment.setCreator(token.getUser().getId().toString());
        boolean insert =true;
        dealerAttachmentMapper.updateDealerAttachment(dealerAttachment);
        dealerInfoService.insertDealerInfo(dealerAttachment,token,DealerAttachment.class,dealerAttachment.getDid());
        return insert;
    }

    @Override
    public List<DealerAttachment> selectByDid(Integer did) throws AdscmSystemException {
        Wrapper<DealerAttachment> wrapper= new EntityWrapper<>();
        wrapper.eq("did",did);
        wrapper.orderBy("",true);
        List<DealerAttachment> dealerAttachments = super.selectList(wrapper);
        for (DealerAttachment dealerAttachment : dealerAttachments) {

            if (AdscmBaseUtils.getInstance().isNotEmpty(dealerAttachment.getPath(),null)){
                String[] s = dealerAttachment.getPath().split("\\\\");
                dealerAttachment.setPathName(s[s.length-5]+"\\"+s[s.length-4]+"\\"+s[s.length-3]+"\\"+ s[s.length-2]+"\\"+s[s.length-1]);
            }

        }
        return dealerAttachments;
    }

    @Override
    public List<DealerAttachment> deleteDealerMembers(Integer id, Token token) {

        return null;
    }
}
